package com.graduation.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.graduation.springbootinit.annotation.AuthCheck;
import com.graduation.springbootinit.common.BaseResponse;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.common.ResultUtils;
import com.graduation.springbootinit.constant.UserConstant;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.model.dto.merchant.MerchantApplicationRequest;
import com.graduation.springbootinit.model.entity.MerchantApplication;
import com.graduation.springbootinit.model.entity.User;
import com.graduation.springbootinit.service.MerchantApplicationService;
import com.graduation.springbootinit.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 商户申请接口
 */
@RestController
@RequestMapping("/merchant/application")
public class MerchantApplicationController {

    @Resource
    private MerchantApplicationService merchantApplicationService;

    @Resource
    private UserService userService;

    /**
     * 提交商户申请
     *
     * @param applicationRequest 申请信息
     * @param request           请求
     * @return 申请记录id
     */
    @PostMapping("/submit")
    public BaseResponse<Long> submitApplication(@RequestBody MerchantApplicationRequest applicationRequest,
            HttpServletRequest request) {
        if (applicationRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User loginUser = userService.getLoginUser(request);
        long result = merchantApplicationService.submitApplication(loginUser.getId(), applicationRequest.getReason());
        return ResultUtils.success(result);
    }

    /**
     * 获取用户申请记录
     *
     * @param request 请求
     * @return 申请记录
     */
    @GetMapping("/my")
    public BaseResponse<MerchantApplication> getMyApplication(HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        MerchantApplication application = merchantApplicationService.getUserApplication(loginUser.getId());
        return ResultUtils.success(application);
    }

    /**
     * 获取申请列表（仅管理员）
     *
     * @param current  当前页码
     * @param pageSize 页面大小
     * @param status  申请状态（null-全部, 0-待审核, 1-审核通过, 2-审核拒绝）
     * @return 申请列表
     */
    @GetMapping("/list")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<MerchantApplication>> listApplications(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long pageSize,
            @RequestParam(required = false) Integer status) {
        Page<MerchantApplication> page = merchantApplicationService.listApplications(current, pageSize, status);
        return ResultUtils.success(page);
    }

    /**
     * 获取待审核的申请列表（仅管理员）
     *
     * @param current  当前页码
     * @param pageSize 页面大小
     * @return 申请列表
     */
    @GetMapping("/list/pending")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<MerchantApplication>> listPendingApplications(
            @RequestParam(defaultValue = "1") long current,
            @RequestParam(defaultValue = "10") long pageSize) {
        return listApplications(current, pageSize, 0);
    }

    /**
     * 审核商户申请（仅管理员）
     *
     * @param applicationId  申请id
     * @param status        审核状态（1-通过，2-拒绝）
     * @param reviewComment 审核意见
     * @param request      请求
     * @return 是否成功
     */
    @PostMapping("/review")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> reviewApplication(
            @RequestParam Long applicationId,
            @RequestParam Integer status,
            @RequestParam(required = false) String reviewComment,
            HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        boolean result = merchantApplicationService.reviewApplication(applicationId, status, reviewComment, 
                loginUser.getId(), loginUser.getUserName());
        return ResultUtils.success(result);
    }
}